package com.irdstudio.efp.flow.service.impl;

import java.util.List;
import java.util.Objects;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.util.UUIDUtil;
import com.irdstudio.efp.flow.service.dao.BizFlowRouteDao;
import com.irdstudio.efp.flow.service.domain.BizFlowRoute;
import com.irdstudio.efp.flow.service.facade.BizFlowRouteService;
import com.irdstudio.efp.flow.service.vo.BizFlowRouteVO;
import com.irdstudio.basic.framework.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 业务流转路由信息				<p>
 * @author ligm
 * @date 2018-05-05
 */
@Service("bizFlowRouteService")
public class BizFlowRouteServiceImpl implements BizFlowRouteService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(BizFlowRouteServiceImpl.class);

	@Autowired
	private BizFlowRouteDao bizFlowRouteDao;

	/**
	 * 新增操作
	 */
	public int insertBizFlowRoute(BizFlowRouteVO inBizFlowRouteVo) {
		logger.debug("当前新增数据为:"+ inBizFlowRouteVo.toString());
		int num = 0;
		try {
			BizFlowRoute bizFlowRoute = new BizFlowRoute();
			beanCopy(inBizFlowRouteVo, bizFlowRoute);
			if ( StringUtils.isEmpty( bizFlowRoute.getBizRouteId() ) ) {
				bizFlowRoute.setBizRouteId(UUIDUtil.getUUID());
			}
			num = bizFlowRouteDao.insertBizFlowRoute(bizFlowRoute);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BizFlowRouteVO inBizFlowRouteVo) {
		logger.debug("当前删除数据条件为:"+ inBizFlowRouteVo);
		int num = 0;
		try {
			BizFlowRoute bizFlowRoute = new BizFlowRoute();
			beanCopy(inBizFlowRouteVo, bizFlowRoute);
			num = bizFlowRouteDao.deleteByPk(bizFlowRoute);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizFlowRouteVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BizFlowRouteVO inBizFlowRouteVo) {
		logger.debug("当前修改数据为:"+ inBizFlowRouteVo.toString());
		int num = 0;
		try {
			BizFlowRoute bizFlowRoute = new BizFlowRoute();
			beanCopy(inBizFlowRouteVo, bizFlowRoute);
			num = bizFlowRouteDao.updateByPk(bizFlowRoute);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizFlowRouteVo +"修改的数据条数为"+ num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public BizFlowRouteVO queryByPk(BizFlowRouteVO inBizFlowRouteVo) {

		logger.debug("当前查询参数信息为:"+ inBizFlowRouteVo);
		try {
			BizFlowRoute queryBizFlowRoute = new BizFlowRoute();
			beanCopy(inBizFlowRouteVo, queryBizFlowRoute);
			BizFlowRoute queryRslBizFlowRoute = bizFlowRouteDao.queryByPk(queryBizFlowRoute);
			if (Objects.nonNull(queryRslBizFlowRoute)) {
				BizFlowRouteVO outBizFlowRouteVo = beanCopy(queryRslBizFlowRoute, new BizFlowRouteVO());
				logger.debug("当前查询结果为:"+ outBizFlowRouteVo.toString());
				return outBizFlowRouteVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowRouteVO> queryAllOwner(BizFlowRouteVO bizFlowRouteVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BizFlowRouteVO> list = null;
		try {
			List<BizFlowRoute> bizFlowRoutes = bizFlowRouteDao.queryAllOwnerByPage(bizFlowRouteVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ bizFlowRoutes.size());
			pageSet(bizFlowRoutes, bizFlowRouteVo);
			list = (List<BizFlowRouteVO>) beansCopy(bizFlowRoutes, BizFlowRouteVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowRouteVO> queryAllCurrOrg(BizFlowRouteVO bizFlowRouteVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BizFlowRoute> bizFlowRoutes = bizFlowRouteDao.queryAllCurrOrgByPage(bizFlowRouteVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+bizFlowRoutes.size());
		List<BizFlowRouteVO> list = null;
		try {
			pageSet(bizFlowRoutes, bizFlowRouteVo);
			list = (List<BizFlowRouteVO>) beansCopy(bizFlowRoutes, BizFlowRouteVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowRouteVO> queryAllCurrDownOrg(BizFlowRouteVO bizFlowRouteVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BizFlowRoute> bizFlowRoutes = bizFlowRouteDao.queryAllCurrDownOrgByPage(bizFlowRouteVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ bizFlowRoutes.size());
		List<BizFlowRouteVO> list = null;
		try {
			pageSet(bizFlowRoutes, bizFlowRouteVo);
			list = (List<BizFlowRouteVO>) beansCopy(bizFlowRoutes, BizFlowRouteVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 根据源节点id查询路由信息
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowRouteVO> queryAllBySourceNodeId(BizFlowRouteVO vo) {
		logger.debug("根据事件ID："+ vo.getBizEventId() +"源节点ID："+ vo.getBizSourceNodeId() +", 业务流转ID："+ vo.getBizFlowId() +"查询路由信息开始......");
		List<BizFlowRouteVO> vos = null;
		try {
			BizFlowRoute bfRoute = beanCopy(vo, new BizFlowRoute());
			List<BizFlowRoute> list = this.bizFlowRouteDao.queryAllBySourceNodeId(bfRoute);
			vos = (List<BizFlowRouteVO>) this.beansCopy(list, BizFlowRouteVO.class);
			logger.debug("根据事件ID："+ vo.getBizEventId() +"源节点ID："+ vo.getBizSourceNodeId() +", 业务流转ID："+ vo.getBizFlowId() +"查询路由信息结束......");
		} catch (Exception e) {
			logger.error("根据事件ID："+ vo.getBizEventId() +"源节点ID："+ vo.getBizSourceNodeId() +", 业务流转ID："+ vo.getBizFlowId() +"查询路由信息或转换出现异常!", e);
		}
		return vos;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BizFlowRouteVO> queryAllByEventId(String flowId, String eventId) {
		logger.debug("根据事件ID："+ eventId +"查询路由信息开始......");
		List<BizFlowRouteVO> vos = null;
		try {
			BizFlowRouteVO vo = new BizFlowRouteVO();
			vo.setBizEventId(eventId);
			vo.setBizFlowId(flowId);
			List<BizFlowRoute> list = this.bizFlowRouteDao.queryAllByEventId(vo);
			vos = (List<BizFlowRouteVO>) this.beansCopy(list, BizFlowRouteVO.class);
			logger.debug("根据事件ID："+ eventId +"查询路由信息结束......");
		} catch (Exception e) {
			logger.error("根据事件ID："+ eventId +"查询路由信息或转换出现异常!", e);
		}
		return vos;
	}
}
